package com.itheima.web;

import cn.hutool.core.lang.UUID;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import com.itheima.utils.JedisUtil;
import com.itheima.utils.QiNiuUtil;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @version V1.0
 * @author: ljx
 * @date: 2020/01/13 16:06
 * @description:
 */
@RestController
@RequestMapping("/setmeal")
public class SetMealController {

    @Reference(cluster = "failfast")
    private SetMealService setMealService;

    @Autowired
    private JedisUtil jedisUtil;

    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile) {
        //获取上传文件的原始文件名
        String originalFilename = imgFile.getOriginalFilename();
        //截取原始文件名的后缀名
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //生成该文件的随机名
        String newFileName = UUID.randomUUID().toString() + suffix;

        //清除脏数据,将用户点击上传图片但未点击确定创建套餐的图片名保存在redis中
        jedisUtil.sadd(RedisConstant.SETMEAL_QINIU_STORE, newFileName);

        try {
            QiNiuUtil.uploadFile(imgFile.getBytes(), newFileName);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,newFileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal){
        Result result = null;

        try {
            result = setMealService.add(setmeal);
            jedisUtil.sadd(RedisConstant.SETMEAL_DB_STORE,setmeal.getImg());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        PageResult pageResult = null;
        try {
            pageResult = setMealService.findPage(pageBean);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            PageResult pageResult1 = new PageResult(0L, null);
            pageResult1.setResult(new Result(false,MessageConstant.GET_SETMEAL_LIST_FAIL));
            return pageResult1;
        }
    }
}
